<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span class="het">合同签订管理</span>
        <el-button style="margin-left: 70%" type="success" size="small" icon="el-icon-plus" @click="openDialog">新增</el-button>
        <el-button type="danger" size="small" icon="el-icon-delete" @click="delBatch">批量删除</el-button>
      </div>
    </el-card>

    <el-form
      ref="searchForm"
      :inline="true"
      :model="keyword"
      class="demo-form-inline"
      style="padding-top: 10px;margin-left: 35px">
      <el-form-item label="租户名称:" prop="conName">
        <el-input style="width: 204px" v-model="keyword.conName" placeholder="请输入租户名称"/>
      </el-form-item>
      <el-form-item label="房间名称:" prop="homeName">
        <el-input style="width: 204px" v-model="keyword.homeName" placeholder="请输入房间名称"/>
      </el-form-item>

      <!--查询-->
      <el-form-item>
        <el-button type="primary" size="small" icon="el-icon-search" @click="search">查询</el-button>
        <el-button type="warning" size="small" icon="el-icon-refresh-right" @click="resetSearchForm('searchForm')">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <!-- 下拉框 -->
    <el-form
      ref="form"
      :inline="true"
      :model="contract"
      class="demo-form-inline"
      style="padding-top: 10px;margin-left: 35px">

      <el-form-item label="所属楼宇:">   <!--   style="margin-left: 15px"     -->
        <el-select v-model="keyword.tenement" placeholder="请选择" @change="search">
          <el-option v-for="roleType in ListKuang" :label="roleType" :value="roleType"/>
        </el-select>
      </el-form-item>

      <el-form-item label="合同类型:">              <!--style="margin-left: 15px"  v-if="show==true"-->
        <el-select v-model="keyword.conType" placeholder="请选择" @change="search">
          <el-option v-for="roleType in ListKuang" :label="roleType" :value="roleType"/>
        </el-select>
      </el-form-item>
    </el-form>

    <!-- 表格数据区域-->
    <el-table :data="contractList" style="width: 100%;margin-bottom: 20px;">
      <el-table-column prop="conName" align="center" label="租户名称"/>
      <el-table-column prop="tenement" align="center" label="所属楼宇"/>
      <el-table-column prop="homeName" align="center" label="房间名称"/>
      <el-table-column prop="conType" align="center" label="合同类型"/>
      <el-table-column prop="conStatus" align="center" label="合同状态"/>
      <el-table-column prop="createTime" align="center" label="签约时间"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleFind(scope.row)"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增/详情 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="44%">
      <el-form :model="contract" :rules="contractRules" ref="contractRulesForm">
        <el-form-item label="id" prop="id">
          <el-input :disabled="true" v-model="contract.id" autocomplete="off" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="合同编号" prop="conNum">
          <el-input v-model="contract.conNum" autocomplete="off" style="width: 300px"></el-input>
        </el-form-item>
<!--        <el-form-item label="签订时间" prop="createTime">-->
<!--          <el-input v-model="contract.createTime" autocomplete="off" style="width: 300px"></el-input>-->
<!--        </el-form-item>-->
        <el-form-item label="租户名称" prop="conName">
          <el-input v-model="contract.conName" autocomplete="off" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="租户类型" prop="zuType">
          <el-input v-model="contract.zuType" autocomplete="off" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="合同属性" prop="conType">
          <el-input v-model="contract.conType" autocomplete="off" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="签订人—" prop="conPeople">
          <el-input v-model="contract.conPeople" autocomplete="off" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="contract.phone" autocomplete="off" style="width: 300px"></el-input>
        </el-form-item>
      </el-form>
<!--      操作选项-->
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <!--   点确定触发函数  进行校验 -->
        <el-button type="primary" @click="addContract('contractRulesForm')">保 存</el-button>
      </div>
    </el-dialog>

    <!--完整功能分页-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[1, 5, 7, 10,15,20]"
      :page-size="5"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

  </div>
</template>

<script>

import { getList, delList, findList, addList, editList } from '@/api/contract'

export default {
  name: 'Contract',
  data() {
    return {
      title: '',
      keyword: { // 添加查询关键词对象
        id: '',
        conName: null,
        homeName: null,
        tenement: null,
        conType: null,
        conStatus: null,
        createTime: ''
      },
      contract: {
        id: '',
        conName: null,
        homeName: null,
        tenement: null,
        conType: null,
        conStatus: null,
        createTime: '',
        zuType: null,
        conPeople: null,
        phone: null
      },
      contractRules: {
        conNum: [
          { required: true, message: '', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '', trigger: 'blur' }
        ],
        conName: [
          { required: true, message: '', trigger: 'blur' }
        ],
        zuType: [
          { required: true, message: '', trigger: 'blur' }
        ],
        conType: [
          { required: true, message: '', trigger: 'blur' }
        ],
        conPeople: [
          { required: true, message: '', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '', trigger: 'blur' }
        ]
      },
      dialogFormVisible: false,
      loading: false,
      ListKuang: [],
      contractList: [],
      currentPage: 1,
      pageNum: 1,
      pageSize: 7,
      total: 0
    }
  },
  methods: {
    fetchData() {
      getList().then(res => {
        this.contractList = res.data
      })
    },


    // fetchData() {
    //   let params = {
    //     pageNum: this.pageNum,
    //     pageSize: this.pageSize,
    //     id: this.keyword.id,
    //     conName: this.keyword.conName,
    //     homeName: this.keyword.homeName
    //   }
    //   getList(params).then(res => {
    //     let pageInfo = res.data
    //     this.loading = false
    //     this.contractList = pageInfo.list
    //     this.total = pageInfo.total
    //   })
    // },

    // 搜索
    search() {
      this.fetchData()
    },
    // 重置
    resetSearchForm(formName) {
      this.$refs[formName].resetFields()
      this.fetchData()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.fetchData()
    },


    // 删除
    handleDelete(row) {
      this.$confirm('是否确认删除住户名称为' + row.conName + '的数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = {
          id: row.id
        }
        delList(params).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.fetchData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 详情
    handleFind(row) {
      this.dialogFormVisible = true
      this.contract = row
      this.title = '确认基本信息'
    },
    // 下拉框
    find() {
      findList().then(res => {
        this.ListKuang = res.data
        this.fetchData()
      })
    },
    // 打开框
    openDialog() {
      this.contract = {}
      this.dialogFormVisible = true
      this.title = '合同基本信息'
    },
    addContract(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.title === '确认基本信息') {
            // editList(this.contract).then(res => {
            this.dialogFormVisible = false
            this.fetchData()
            // })
          } else if (this.title === '合同基本信息') {
            addList(this.contract).then(res => {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.dialogFormVisible = false
              this.fetchData()
            })
          }
        } else {
          return false
        }
        this.dialogFormVisible = false
      })
    }
  },
  created() {
    this.fetchData()
  }
}
</script>

<style>
/*.clearfix:before,*/
/*.clearfix:after {*/
/*  display: table;*/
/*  content: "";*/
/*}*/

/*.clearfix:after {*/
/*  clear: both*/
/*}*/

.box-card {
  width: 100%;
}

.het {
  font-size: 24px;
}
</style>
